package com.sinocare.project.report.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.project.common.domain.PercentPass;
import com.sinocare.project.common.domain.SccReprot;
import com.sinocare.project.common.domain.SccResult;
import com.sinocare.project.common.domain.Store;
import com.sinocare.project.common.domain.vo.SccMedicalResultVo;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.report.service.SccResultService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/resultmanager")
public class SccResultController {

    @Resource
    private SccResultService sccResultService;

    @ApiOperation(value = "查询所有检测结果",notes = "查询所有检查结果的信息",httpMethod = "GET")
    @RequestMapping(value = "findResult",method = RequestMethod.GET)
    public Result pageStores(Integer page, Integer limit, String searchParams)throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        SccMedicalResultVo resultVo=null;
        if (searchParams!=null){
            resultVo=objectMapper.readValue(searchParams,SccMedicalResultVo.class);
        }
        if (resultVo != null) {
            if (resultVo.getSubStore() != null && resultVo.getSubStore() != "") {
                System.out.println("substore");
                resultVo.setStoreCode(resultVo.getSubStore());
            } else {
                System.out.println("substore2");
                if (resultVo.getHeadStore() != null) {
                    System.out.println("headstore");
                    resultVo.setStoreCode(resultVo.getHeadStore());
                }
            }

        }
//        System.out.println("resultVo++++++++++"+resultVo);
        Page<Object> page1 = PageHelper.startPage(page,limit);
        System.out.println(page1);
        List<SccResult> sccResults = sccResultService.queryResult(resultVo);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),sccResults);
    }


    @ApiOperation(value = "查询所有总店名",notes = "查询出所有总店名",httpMethod = "GET")
    @RequestMapping(value = "stname",method = RequestMethod.GET)
    public Result selectStoreNames()throws Exception{
        List<Store> s = sccResultService.selectStoreName();
        return Result.ok(ResultCode.SUCCESS,s);
    }

    @ApiOperation(value = "查询某个总店下的所有分店名",notes = "查询出某个总店下的所有分店名",httpMethod = "GET")
    @RequestMapping(value = "subStoret",method = RequestMethod.GET)
    public Result selectsubStoreNames(@PathParam("headStore") String storeCode)throws Exception{
        List<Store> s = sccResultService.selectsubStoreName(storeCode);
        return Result.ok(ResultCode.SUCCESS,s);
    }

    @ApiOperation(value = "查询药店检测量",notes = "查询出药店检测量",httpMethod = "GET")
    @RequestMapping(value = "selectResult",method = RequestMethod.GET)
    public Result selectResults(@PathParam("storeCode") String storeCode)throws Exception{
        List<SccResult> sccResults = sccResultService.selectResult(storeCode);
        return Result.ok(ResultCode.SUCCESS,sccResults);
    }


    @ApiOperation(value = "查询药店检测人数",notes = "查询出药店检测人数",httpMethod = "GET")
    @RequestMapping(value = "selectCount",method = RequestMethod.GET)
    public Result selectCount(@PathParam("storeCode") String storeCode)throws Exception{
        List<SccResult> sccResults = sccResultService.selectCount(storeCode);
        return Result.ok(ResultCode.SUCCESS,sccResults);
    }

    @ApiOperation(value = "查询合格率",notes = "查询合格率",httpMethod = "GET")
    @RequestMapping(value = "selecthg",method = RequestMethod.GET)
    public Result selecthg(@PathParam("storeCode") String storeCode)throws Exception{
        List<PercentPass> selecthg = sccResultService.selecthg(storeCode);
        for (PercentPass integer : selecthg) {
            System.out.println(integer);
        }

        return Result.ok(ResultCode.SUCCESS,selecthg);
    }



    @ApiOperation(value = "查询每日检测人数",notes = "查询每日检测人数",httpMethod = "GET")
    @RequestMapping(value = "selectRs",method = RequestMethod.GET)
    public Result selectRs(SccMedicalResultVo resultVo)throws Exception{

        if (resultVo != null) {
            if (resultVo.getSubStore() != null && resultVo.getSubStore() != "") {
//                System.out.println("substore");
                resultVo.setStoreCode(resultVo.getSubStore());
            } else {
//                System.out.println("substore2");
                if (resultVo.getHeadStore() != null) {
                    System.out.println("headstore");
                    resultVo.setStoreCode(resultVo.getHeadStore());
                }
            }

        }

        List<Integer> integerList = sccResultService.selectRs2(resultVo);
        for (Integer integer : integerList) {
            System.out.println(integer);
        }
        return Result.ok(ResultCode.SUCCESS,integerList);
    }


    @ApiOperation(value = "查询各项合格人数",notes = "查询各项的合格人数",httpMethod = "GET")
    @RequestMapping(value = "queryReprot",method = RequestMethod.GET)
    public Result queryReprot(@PathParam("storeCode") String storeCode)throws Exception{
        List<PercentPass> sccReprots = sccResultService.queryReprot(storeCode);
        for (PercentPass sccReprot : sccReprots) {
            System.out.println(sccReprot);
        }
        System.out.println(sccReprots);
        return Result.ok(ResultCode.SUCCESS,sccReprots);
    }

    @ApiOperation(value = "查询各项指标异常人数",notes = "查询各项的指标异常人数",httpMethod = "GET")
    @RequestMapping(value = "selectRl",method = RequestMethod.GET)
    public Result selectRl(@PathParam("storeCode") String storeCode)throws Exception{
        List<PercentPass> percentPasses = sccResultService.selectRl(storeCode);

        return Result.ok(ResultCode.SUCCESS,percentPasses);
    }

}
